package blog.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

public class VerifyCode {
    private static final String ALPHANUMERIC = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789";
    private static final String[] FONTNAMES = {"宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312"};
    private Random random = new Random();
    private int width;
    private int height;
    private Color bgColor = new Color(255,255,255);
    private VerifyCode(){}

    public static String getCodes(int digit){
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for(int i=0;i<digit;i++) {
            int index = random.nextInt(ALPHANUMERIC.length());
            code.append(ALPHANUMERIC.charAt(index));
        }
        return code.toString();
    }

    public static BufferedImage getCodeImg(String code, int width, int height){
        VerifyCode vc = new VerifyCode();
        vc.width = width;
        vc.height = height;

        BufferedImage image = vc.createImage(); //创建图片缓冲区
        Graphics2D g2 = (Graphics2D)image.getGraphics();//得到绘制环境，画笔
        // 向图片中画字符
        for(int i = 0; i < code.length(); i++)  {
            g2.setFont(vc.randomFont());  //设置随机字体
            g2.setColor(vc.randomColor()); //设置随机颜色
            int x = (int)(i * 1.0F * width / code.length()); //设置当前字符的x轴坐标
            int y = height - 5;
            g2.drawString(String.valueOf(code.charAt(i)), x, y);  //画图
        }
        vc.drawLine(image); //添加干扰线
        return image;
    }

    //保存图片到指定的输出流
    public static void output (BufferedImage image, OutputStream out)
            throws IOException {
        ImageIO.write(image, "JPEG", out);
    }


    //画干扰线
    private void drawLine (BufferedImage image) {
        int num  = 3 + random.nextInt(5); //总共画三条干扰线
        Graphics2D g2 = (Graphics2D)image.getGraphics();
        for(int i = 0; i < num; i++) { //生成两个点的左边，即4个值
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            g2.setStroke(new BasicStroke(1.5F));
            g2.setColor(randomColor());   //设置干扰线颜色为蓝色
            g2.drawLine(x1, y1, x2, y2);
        }
    }
    //创建BufferedImage
    private BufferedImage createImage () {
        BufferedImage image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D)image.getGraphics();
        g2.setColor(this.bgColor);
        g2.fillRect(0, 0, this.width, this.height);
        return image;
    }
    private Color randomColor(){
        Random random = new Random();
        int r = random.nextInt(150);
        int g = random.nextInt(150);
        int b = random.nextInt(150);
        return new Color(r, g, b);
    }
    private Font randomFont(){
        Random random = new Random();
        String fontName = FONTNAMES[random.nextInt(FONTNAMES.length)];
        int style = random.nextInt(4);  //生成随机样式，0:无样式，1:粗体，2:斜体，3:粗体+斜体
        int size = random.nextInt(5) + 24; //生成随机字号
        return new Font(fontName, style, size);
    }

}
